package view;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;

import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import VOs.CasillaVO;

import model.ModeloServidorCorreo;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class FrameCasilla extends javax.swing.JFrame {
	private JPanel panel;
	private JButton jButtonEliminar;
	private JScrollPane jScrollPaneMateriales;
	private JButton jButtonGuardar;
	private JList jListCasillas;
	private JTextField jTextNombre;
	private JLabel jLabelNombre;
	private DefaultListModel listModel;
	
	private ModeloServidorCorreo modelo;
	
	private Vector<CasillaVO> casillas;

	private static final FrameCasilla INSTANCE = new FrameCasilla();
	
	private FrameCasilla(){super();}
	
	public static FrameCasilla getInstancia(){
		return INSTANCE;
	}
	
	public void init(ModeloServidorCorreo modelo) {
		this.modelo = modelo;
		initGUI();
		initData();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setLayout(null);
			this.setTitle("Casillas de Correo");
			{
			panel = new JPanel();
				getContentPane().add(panel, BorderLayout.CENTER);
				panel.setBounds(0, 0, 392, 266);
				panel.setLayout(null);
				{
					jLabelNombre = new JLabel();
					panel.add(jLabelNombre);
					jLabelNombre.setText("Nombre");
					jLabelNombre.setBounds(12, 12, 60, 14);
				}
				{
					jTextNombre = new JTextField();
					panel.add(jTextNombre);
					jTextNombre.setBounds(61, 9, 319, 21);
				}
				{
					jButtonGuardar = new JButton();
					panel.add(jButtonGuardar);
					jButtonGuardar.setText("Guardar");
					jButtonGuardar.setBounds(61, 234, 86, 21);
					jButtonGuardar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButtonGuardarActionPerformed(evt);
						}
					});
				}
				{
					jButtonEliminar = new JButton();
					panel.add(jButtonEliminar);
					jButtonEliminar.setText("Eliminar");
					jButtonEliminar.setBounds(224, 234, 86, 21);
					jButtonEliminar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButtonEliminarActionPerformed(evt);
						}
					});
					jButtonEliminar.setEnabled(false);
				}
				{
					jScrollPaneMateriales = new JScrollPane();
					panel.add(jScrollPaneMateriales);
					jScrollPaneMateriales.setBounds(13, 35, 367, 187);
					{
						jListCasillas = new JList();
						jScrollPaneMateriales.setViewportView(jListCasillas);
						jListCasillas.addListSelectionListener(new ListSelectionListener() {
							public void valueChanged(ListSelectionEvent evt) {
								jListCasillasValueChanged(evt);
							}
						});
					}
				}
			}
			pack();
			setSize(400, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void initData(){
		listarCasillas();
	}
	
	private void listarCasillas(){
		this.casillas = modelo.getCasillas();
		this.listModel = new DefaultListModel();
		for(CasillaVO c : casillas){
			listModel.addElement(c.getNombre());			
		}
		
		jListCasillas.setModel(listModel);
	}
	
	private CasillaVO buscarCasilla(String nombre){
		for (CasillaVO c : casillas){
			if(c.getNombre().equalsIgnoreCase(nombre))
				return c;
		}		
		return null;
	}
	private void jButtonGuardarActionPerformed(ActionEvent evt) {
		System.out.println("jButtonGuardar.actionPerformed, event="+evt);
		CasillaVO casilla = new CasillaVO();
		casilla.setNombre(jTextNombre.getText());
		modelo.crearCasilla(casilla);
		listarCasillas();
	}
	
	private void jButtonEliminarActionPerformed(ActionEvent evt) {
		System.out.println("jButtonEliminar.actionPerformed, event="+evt);
		CasillaVO casilla = buscarCasilla(jListCasillas.getSelectedValue().toString());
		if(casilla != null){
			modelo.eliminarCasilla(casilla);
			listarCasillas();
			jListCasillas.setSelectedIndex(0);
		}
	}
	
	private void jListCasillasValueChanged(ListSelectionEvent evt) {
		System.out.println("jListCasillas.valueChanged, event="+evt);
		jButtonEliminar.setEnabled(true);
		if(jListCasillas.getSelectedValue()!= null){
			jTextNombre.setText(jListCasillas.getSelectedValue().toString());
		}
	}

}
